﻿using JT809.Protocol;
using JT809.Protocol.Configs;
using JTT809_2011_Service.DotnettyExtensions;
using JTT809_2011_Service.Extensions;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using RabbitMqHelp.Extensions;
using System;
using System.Threading.Tasks;

namespace JTT809_2011_Service
{
    class Program
    {
        /// <summary>
        /// 程序入口
        /// </summary>
        /// <param name="args"></param>
        static async Task Main(string[] args)
        {
            var serverHostBuilder = new HostBuilder()
                .ConfigureAppConfiguration((hostingContext, config) =>
                {
                    //读取配置文件
                    config.SetBasePath(AppDomain.CurrentDomain.BaseDirectory);
                    //热更新文件
                    config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
                })
                .ConfigureLogging((context, logging) =>
                {
                    //配置日志
                    logging.AddConsole();
                    logging.SetMinimumLevel(LogLevel.Trace);
                })
                .ConfigureServices((hostContext, services) =>
                {
                    //注入服务
                    //单例方式注入
                    services.AddSingleton<ILoggerFactory, LoggerFactory>();
                    services.AddSingleton(typeof(ILogger<>), typeof(Logger<>));
                    //注入JT809请求头信息
                    //services.AddSingleton(new JT809HeaderOptions
                    //{
                    //    MsgGNSSCENTERID = 2020,
                    //    Version = new JT809Header_Version(1, 0, 0)
                    //});
                    //注入配置文件信息
                    services.AddJT809HeaderOptions(hostContext.Configuration);
                    services.AddIPEndPointConfig(hostContext.Configuration);
                    services.AddIJT809_0x1001Options(hostContext.Configuration);

                    services.AddJT809Configure()
                            .AddJT809Core(hostContext.Configuration)
                            .AddJT809InferiorPlatformClient();
                    services.AddHostedService<JT809InferiorService>();


                    services.AddRabbitMqServer(hostContext.Configuration);
                });
            await serverHostBuilder.RunConsoleAsync();

        }
    }
}
